package cn.edu.ccnu.nlp;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.MasterNotRunningException;
import org.apache.hadoop.hbase.ZooKeeperConnectionException;
import org.apache.hadoop.hbase.client.HBaseAdmin;
import org.apache.hadoop.hbase.util.Bytes;

public class TableCreate {
	
	public static Configuration configuration;
	static {
		configuration = HBaseConfiguration.create();
		//configuration.set("hbase.zookeeper.property.clientPort", "2181");
		//configuration.set("hbase.zookeeper.quorum", "192.168.1.100");
		//configuration.set("hbase.master", "192.168.1.100:600000");
	}
	
	/**
	 * 创建news表
	 * @param tablename
	 * @throws IOException
	 */
	public void createNews (String tablename) throws IOException {	 
		 HBaseAdmin admin = new HBaseAdmin(configuration);
		 HTableDescriptor tabledescriptor = new HTableDescriptor(Bytes.toBytes("news"));
		 
		 tabledescriptor.addFamily(new HColumnDescriptor ("title"));
		 tabledescriptor.addFamily(new HColumnDescriptor ("author"));
		 tabledescriptor.addFamily(new HColumnDescriptor ("date"));
		 tabledescriptor.addFamily(new HColumnDescriptor ("source"));
		 tabledescriptor.addFamily(new HColumnDescriptor ("text"));
		 
		 admin.createTable(tabledescriptor);
		 
		 System.out.println("news表创建成功！");
		 
		}

	/**
	 * 创建news_stat表
	 * @param tablename
	 * @throws IOException
	 */
	public void createNewsStat (String tablename) throws IOException {	 
		 HBaseAdmin admin = new HBaseAdmin(configuration);
		 HTableDescriptor tabledescriptor = new HTableDescriptor(Bytes.toBytes("news_stat"));
	
		 tabledescriptor.addFamily(new HColumnDescriptor ("num"));		 
		 admin.createTable(tabledescriptor);
		 System.out.println("news_stat表创建成功！");
			 
		}
	
	/**
	 * 建立news的模拟时间索引的表news_index，以date为rowkey，加快查询
	 * @param tableName
	 * @throws IOException
	 */
	public void createNewsIndex(String tableName) throws IOException{
			HBaseAdmin admin = new HBaseAdmin(configuration);
			HTableDescriptor tabledescriptor = new HTableDescriptor(Bytes.toBytes("news_index"));
			tabledescriptor.addFamily(new HColumnDescriptor("news"));		 
			admin.createTable(tabledescriptor);
			System.out.println("news_index表创建成功！");
	}
	
	
	public static void main(String[] args){
		TableCreate tc = new TableCreate();
		try{
//			tc.createNews("news");
//			tc.createNewsStat("news_stat");
			tc.createNewsIndex("news_index");
		}catch(Exception e){
			e.getStackTrace();
		}
	}
}
